<?php

require('config.php');

//Vérifier que l'utilisateur est connecté

if(isset($_SESSION['id_user'])&&!isTranslating()) {
    redirectFront('index');
}
$inscriptionByUrl = modelProvince::getProvinceByUrl('inscription.php');
	if($inscriptionByUrl){
		$balise_title = $inscriptionByUrl->title;
		$description = $inscriptionByUrl->meta_title;
		$id_menu_left_tree = modelProvince::getProvinceMenuleft($inscriptionByUrl->language_link)->id_menu_left_tree;
	}else{
		$balise_title = 'inscription_balise';
		$description = 'inscription_description';
	}
$page = new rootLayoutPage();
$page->setTranslatePage(__FILE__);
$page->setBaliseTitle($balise_title);
$page->setDescription($description);
$page->addJavaScript('jquery.validate.js');
$page->addJavaScript('jquery.inscrioption.js');
$page->addJavaScript('jQuery.jPopup.js');
$page->addStyleSheet('jPopup.css');


$xml = tmxTranslate('inscription.xml', REP_XLAT_TEMPLATE);
$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
//-- menu Left show products--------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('connexion');
$menuPage->setIdShow($id_menu_left_tree);
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$header = new layoutHeader();
$header->setIdShow($id_menu_left_tree);
$page->setHeader($header);

$boo = false;
if(isset($_SESSION['go']) == 1){
	unset($_SESSION['go']);
	redirect('index');
}

if(isset($_POST['login_user'])){
	try {
	$email_mail = addslashes($_POST['login_user']);
	$nom = addslashes($_POST['nom']);
    $prenom = addslashes($_POST['prenom']);
	$city = addslashes($_POST['city']);
	$id_country = addslashes($_POST['id_country']);
	//$phone_number = addslashes($_POST['phone_number']);
	$i_am = addslashes($_POST['i_am']);
	$id_titri = addslashes($_POST['id_titri']);
	if($i_am == 1) $credit = DEFAULT_CREDIT_PARTICULAR;
	else $credit = DEFAULT_CREDIT_PROFESSIONAL;

	$check_mail = isset($_POST['ch_email'])?1:0;
	$check_name = isset($_POST['ch_name'])?1:0;
	$check_city = isset($_POST['ch_city'])?1:0;
	$check_country = isset($_POST['ch_country'])?1:0;
	$check_phone = isset($_POST['ch_phone'])?1:0;
	
	$agency=addslashes($_POST['agency']);;
	$website=addslashes($_POST['website']);
	 if(isValidEmail($email_mail)) {
		if(!modelUser::existLogin($email_mail)){
			//if(isValidEmail($email_mail)){
			if(!is_numeric($i_am) or $i_am <= 0 or $i_am == '')  throw new Exception('');
				$user = modelUser::inscription($email_mail);
				$id_infos = modelUser::insertInfo($user->id_user,$nom,$prenom,$city,$id_country,$i_am,$credit,$check_mail,$check_name,$check_city,$check_country,$id_titri,$agency,$website);
				if($credit > 0) modelUser::insertCreditForUser($user->id_user,$credit,0);
				$listPhones = modelUser::getListCountryLanguage();

				for($i=1;$i<$_POST['max_phone']+1;$i++){
					$phone = addslashes($_POST[$i.'-phone_number']);
					//$checkPhone = isset($_POST[$i.'-ch_phone'])?1:0;
					$id_phone_language = '';
					foreach($listPhones as $kp => $phones){
						$var = '-check_phones'.$phones->id_country_language;
						 if(isset($_POST[$i.$var])){
						 	$id_phone_language .= $phones->id_country_language.',';
						 }
					}
					$id_phone = substr($id_phone_language, 0, -1);
					if($phone == '' or $id_phone == '') throw new Exception('');
					else modelUser::insertPhoneLanguageUser($id_infos, $phone,$id_phone,1);
				}
						$mail = new flyLayout(REP_MAIL.'inscription.mail.tpl');
						$mail->start();
						$mail->setVariable('mail_utilisateur', $email_mail);
						$mail->setVariable('lien_activation', 'http://'.$_SERVER['HTTP_HOST'].LANGUAGE_FOLDER.'/validation.php?id='.$user->id_user.'&clef='.$user->key_unique);
						$mail->stop();

						$subject = $xml_subject['inscription'];

						if(STATUS_EMAIL == 0) {
			        		echo $mail;
			        	}elseif(STATUS_EMAIL == 1) {
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}else {
			        		echo $mail;
			        		monsterMail($email_mail, $subject, $mail);
			        		monsterMail(MAIL_WEBMASTER, $subject, $mail);
			        	}

						/*$mail = new flyLayout(REP_MAIL.'inscription.mail.tpl');
						$mail->start();
						$mail->setVariable('mail_utilisateur', $email_mail);
						$mail->setVariable('lien_activation', 'http://'.$_SERVER['HTTP_HOST'].'/validation.php?id='.$user->id_user.'&clef='.$user->key_unique);
						$mail->stop();*/
			    $_SESSION['go'] = 1;
				$boo = true;
			//}else throw new Exception($xml['error_message_1']);
		}else throw new Exception($xml['error_message_1']);
	}else throw new Exception($xml['error_message_1']);
	} catch (Exception $e) {

		Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message :: ERROR));
	}
}

$tpl_file='inscription.tpl';
$inscription = new flyLayout(REP_TPL . $tpl_file);
$inscription->start();
if($boo == true) $inscription->showBlock('success');
else $inscription->showBlock('inscription');
	$listIam = modelUser::getListIam();
	foreach($listIam as $k => $iAm){
		$name = 'i_am_'.$_SESSION['language_code'];
		$inscription->setVariable('checked',$iAm->id_i_am == isset($_POST['i_am'])?'CHECKED="CHECKED"':'');
		$inscription->setVariable('id_i_am',$iAm->id_i_am);
		$inscription->setVariable('name_i_am',$iAm->$name);
		$inscription->parseList('i_am');
	}

	$listTitre = modelUser::getListTitre();
	$id_titre = isset($_POST['id_titre'])?$_POST['id_titre']:'';
	foreach($listTitre as $kt => $titre){
		$titri_name = 'titre_court_'.$_SESSION['language_code'];
		$inscription->setVariable('titri_checked',($titre->id_titre == $id_titre)?'CHECKED="CHECKED"':'');
		$inscription->setVariable('id_titri',$titre->id_titre);
		$inscription->setVariable('name_titri',$titre->$titri_name);
		$inscription->parseList('titri');
	}

	$listCountry = modelUser::getListCountry();
	foreach($listCountry as $kk => $country){
		$name = 'nom_pays_'.$_SESSION['language_code'];
		$inscription->setVariable('selection',$country->id_pays == (isset($_POST['id_country'])?$_POST['id_country']:115)?'selected="selected"':'');
		$inscription->setVariable('id_country',$country->id_pays);
		$inscription->setVariable('name_country',$country->$name);
		$inscription->parseList('list_country');
	}
	$inscription->setVariable('name_check', 'checked="checked"');
	$inscription->setVariable('city_check', 'checked="checked"');
	$inscription->setVariable('country_check', 'checked="checked"');
	//$inscription->setVariable('ch_email', 'checked="checked"');
	
	/*$inscription->setVariable('name_check', $country->public_name==''?'checked="checked"':'');
	$inscription->setVariable('city_check', $country->public_city==''?'checked="checked"':'');
	$inscription->setVariable('country_check', $country->public_country==''?'checked="checked"':'');*/
	
	$inscription->setVariable('nom',isset($_POST['nom'])?$_POST['nom']:'');
	$inscription->setVariable('prenom',isset($_POST['prenom'])?$_POST['prenom']:'');
	$inscription->setVariable('city',isset($_POST['city'])?$_POST['city']:'');
	$inscription->setVariable('check',isset($_POST['check'])?'CHECKED="CHECKED"':'');
/*
	$inscription->setVariable('email_check', $country->public_email==''?'checked="checked"':'');
	//$inscription->setVariable('phone_check', $country->public_phone==''?'checked="checked"':'');

	$inscription->setVariable('phone_number',$_POST['phone_number']);
	$inscription->setVariable('login_user',$_POST['login_user']);

	$listPhone = modelUser::getListPhoneLanguageDefaulf(LANGUAGE_PHONE_SPEAK_DEFAULT);
	foreach($listPhone as $k => $phone){
		$phone_name = language_.$_SESSION['language_code'];
		$inscription->setVariable('name_phone_language',$phone->$phone_name);
		$inscription->setVariable('id_phone_language',$phone->id_country_language);
		$inscription->parseList('list_phone_language');
	}*/
	//$inscription->setVariable('rep_ajax', REP_AJAX);
	include('inscription_contact.php');
    $inscription->includeFile('contact', $inscription_contact);
$inscription->stop();

$page->setBody($inscription);
$page->display();

?>